package com.beck.code.generator.vo;

import jakarta.persistence.*;
import lombok.Data;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author CuiJianbo
 * @since 2025/7/2
 */
@Data
public class EntityFieldVO {
    private String fieldType;
    private String fieldName;

    public static EntityFieldVO getEntityFieldVO(Field field) {

        String fieldType = field.getType().getSimpleName();
        if (
                field.getAnnotation(ManyToMany.class) != null
                        || field.getAnnotation(OneToMany.class) != null
        ) {
            Type entity = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
            fieldType = "List<" + ((Class) entity).getSimpleName() + ">";
        }

        EntityFieldVO entityFieldVO = new EntityFieldVO();
        entityFieldVO.setFieldName(field.getName());
        entityFieldVO.setFieldType(fieldType);
        return entityFieldVO;
    }
}
